package LexPortal.DaoImpl;

import java.util.List;

import LexPortal.DAOInterface.*;
import LexPortal.Entity.User;
import java.sql.*;

public class UserDaoImpl implements UserDAO {

	@Override
	public void Insert(User user) throws Exception {
		String sql = "Insert Into User(name, password, address, email, phone) values(?, ?, ?, ?, ?)";
		String[] parameters = new String[5];
		parameters[0] = user.getName();
		parameters[1] = user.getPassword();
		parameters[2] = user.getAddress();
		parameters[3] = user.getEmail();
		parameters[4] = user.getPhone();

		int retCount = SqliteHelper.ExecuteNonQuery(DBConsts.SqliteFilePath,
				sql, parameters);
		if (retCount != 1)
			throw new Exception("UserDaoImpl Insert: Insert User to DB Failed.");
	}

	@Override
	public void Update(User user) throws Exception {
		String sql = "Update User Set name = ?, password = ?, address = ?," +
				"address = ?, email = ?, phone = ? where id = ?";
		
		String[] parameters = new String[6];
		parameters[0] = user.getName();
		parameters[1] = user.getPassword();
		parameters[2] = user.getAddress();
		parameters[3] = user.getEmail();
		parameters[4] = user.getPhone();
		parameters[5] = Long.toString(user.getId());
		
		int retCount = SqliteHelper.ExecuteNonQuery(DBConsts.SqliteFilePath,
				sql, parameters);
		if (retCount != 1)
			throw new Exception("UserDaoImpl Insert: Insert User to DB Failed.");

	}

	@Override
	public void Delete(long id) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public User QueryById(long id) throws Exception {
		String sql = "Select * From User where id = ?";
		String[] parameters = new String[1];
		parameters[0] = Long.toString(id);
		ResultSet rs = SqliteHelper.ExecuteResultSet(DBConsts.SqliteFilePath,
				sql, parameters);
		User user = null;
		if (rs.next()) {
			user = new User();
			long uid = rs.getLong("id");
			String name = rs.getString("name");
			String password = rs.getString("password");
			String address = rs.getString("address");
			String email = rs.getString("email");
			String phone = rs.getString("phone");
			user.setId(uid);
			user.setName(name);
			user.setPassword(password);
			user.setAddress(address);
			user.setEmail(email);
			user.setPhone(phone);
		}
		return user;
	}

	@Override
	public List<User> QueryAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> QueryAllByPage(int start, int pageNumber,
			String orderField, String orderDirection) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User Login(String name, String password) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User QueryByName(String name) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
